define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'dd', title: __('单独通道'), visible:false,searchList: {"0":__('关闭'),"1":__('开启')}},
                        {field: 'email', title: __('Email'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'qq', title: __('QQ')},
                        {field: 'status', title: __('Status'),searchList: {"hidden":__('关闭'),"normal":__('开启')}, formatter: Controller.api.formatter.status},
                        {field: 'money', title: __('余额')},
                        {field: 'qqpay_rate', title: __('费率'),operate:false,formatter: Controller.api.formatter.qq_rate},
                        {field: 'api_id', title: __('Api_id'),formatter: Controller.api.formatter.api_id},
                        {field: 'api_key', title: __('Api_key'),formatter: Controller.api.formatter.api_key},
                        {field: 'successions', title: __('Successions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'maxsuccessions', title: __('Maxsuccessions'), visible: false, operate: 'BETWEEN', sortable: true},
                        {field: 'operate', title: __('Operate'),table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                showToggle: false,
                showColumns: false,
                showExport: false,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
          	$(document).on("click", ".pay-append", function (e) {
                var name = $(this).parent().parent().data('name');
                var num = parseInt(Math.random()*(99999-10+1)+10,10);
                var more = '';
                var h5 ='';
                if (name == 'row[wxpay_config]') var h5 = '<select name="'+name+'['+num+'][h5]" class="form-control"> <option value="1">H5开启</option> <option value="0">H5关闭</option> </select>';
                if (name == 'row[wxpay_config]') var more = '<input type="text" name="'+name+'['+num+'][appsecret]" class="form-control" value="" size="20"  placeholder="易支付不填/微信AppSecret">';
                if (name == 'row[wxpay_config]') {
                    var placeholder1 = ' placeholder="易支付网址/微信APPID"';
                    var placeholder2 = ' placeholder="易支付ID/微信MCHID"';
                    var placeholder3 = ' placeholder="易支付Key/微信KEY"';
                }
                if (name == 'row[qqpay_config]') {
                    var placeholder1 = ' placeholder="易支付网址/QQ ID"';
                    var placeholder2 = ' placeholder="易支付ID/QQ随便填"';
                    var placeholder3 = ' placeholder="易支付key/QQ key"';
                }
                if (name == 'row[alipay_config]') {
                    var placeholder1 = ' placeholder="易支付网址/支付宝ID"';
                    var placeholder2 = ' placeholder="易支付ID/支付宝邮箱"';
                    var placeholder3 = ' placeholder="易支付Key/支付宝Key"';
                }
              	var selectmore = '';
              	if(name == 'row[wxpay_config]'){
                	var selectmore = '<option value="ysh">易商户</option>';
                }
                var html = '<dd class="form-inline"> <select name="'+name+'['+num+'][type]" class="form-control"> <option value="auth">官方</option> <option value="easy">易支付</option>  '+selectmore+'</select> '+h5+' <input type="text" name="'+name+'['+num+'][url_id]" class="form-control" value="" size="20"'+placeholder1+'> <input type="text" name="'+name+'['+num+'][id_email]" class="form-control" value="" size="20"'+placeholder2+'> <input type="text" name="'+name+'['+num+'][key]" class="form-control" value="" size="20"'+placeholder3+'> '+more+'<span class="btn btn-sm btn-danger btn-remove"><i class="fa fa-times"></i></span> </dd>';
                
                $(this).parent().parent().find('dd[class="form-inline"]:last').after(html);
            });
            Controller.api.bindevent();
        },
        api: {
            formatter: {
                api_id: function (value, row, index) {
                    return (!value ? '无' : value);
                },
                api_key: function (value, row, index) {
                    return (!value ? '无' : value);
                },
                qq_rate: function (value, row, index) {
                    if (!row.qqpay_rate && !row.wxpay_rate && !row.alipay_rate) {
                        return '默认';
                    }
                    return '自定义';
                },
                pid: function (value, row, index) {
                    return (!value ? '无' : '<a data-original-title="上级用户信息（可编辑）" class="btn-execute btn-dialog" href="/admin/user/user/edit?ids='+value+'">'+value+'</a>');
                },
                status: function (value, row, index) {
                    return '<a class="btn-change text-success" data-url="user/user/status?status=' + (row.status == 'hidden' ? 'normal' : 'hidden') + '" data-id="' + row.id + '"><i class="fa ' + (row.status == 'hidden' ? 'fa-toggle-off' : 'fa-toggle-on') + ' fa-2x"></i></a>';
                },
            },
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});